package com.hjk.common.utils.check;

import com.hjk.common.utils.string.JsonUtils;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class CheckCustomerInfoUtil {
    /**
     * 大陆手机
     */
    private static final String REGEX_MOBILE = "^[1][3-9]\\d{9}$";
    /**
     * 固话
     */
    private static final String REGEX_TELEPHONE = "^(0\\d{2}(-?)\\d{7,8}(\\d{1,4})?)|(0\\d{3}(-?)\\d{7,8}(\\d{1,4})?)$";
    /**
     * 中国澳门
     */
    private static final String REGEX_MACAO_PHONE = "^(\\+853)[6]([8|6])\\d{5}$";
    /**
     * 中国香港
     */
    private static final String REGEX_HONGKONG_PHONE = "^(\\+852)([6|9])\\d{7}$";
    /**
     * 中国台湾
     */
    private static final String REGEX_TAIWAN_PHONE = "^(\\+886)[0][9]\\d{8}$";
    /**
     * 大陆身份证
     */
    private static final String REGEX_MAINLAND_ID = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
    /**
     * 港澳同胞回乡证
	 * 	第1位为字母，“H”字头签发给香港居民，“M”字头签发给澳门居民；
	 * 	第2位至第11位为数字，前8位数字为通行证持有人的终身号，
	 * 	后2位数字表示换证次数，首次发证为00，此后依次递增。 H1234567801
     */
    private static final String REGEX_HM_REVIEW_ID = "^([HM])(\\d{10})$";
    /**
     * 护照
     */
    private static final String REGEX_PASSPORT = "^1[45][0-9]{7}|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff|Ee|Pp]\\d{7,8})|([H|h|M|m]\\d{8,10})";
    /**
     * 香港居民身份证 A123456(A)
     */
    private static final String REGEX_HONGKONG_ID = "[A-Z]{1,2}[0-9]{6}\\([0-9|A-Z|a-z]\\)";
    /**
     * 澳门居民身份证 开头是1 5 7 + 6位数字 + (A)
     */
    private static final String REGEX_MACAO_ID = "^[1|5|7][0-9]{6}\\([0-9|A-Z|a-z]\\)";
    /**
     * 台湾居民来往大陆通行证
     */
    private static final String REGEX_TAIWAN_REVIEW_ID = "(\\d{10})\\([0-9|A-Z|a-z]\\)";

	/**
	 * 港澳台手机号码
	 *
	 * 中国大陆：开头1 3-8号段，后边跟9位数字
	 *
	 * 台湾：09开头后面跟8位数字
	 *
	 * 香港：9或6开头后面跟7位数字
	 *
	 * 澳门：66或68开头后面跟5位数字
	 */
	private static final String REGEX_GAT = "^[1][3-8]\\d{9}$|^([6|9])\\d{7}$|^[0][9]\\d{8}$|^[6]([8|6])\\d{5}$";


    private Pattern compileMobile = Pattern.compile(REGEX_MOBILE);
    private Pattern compileTelephone= Pattern.compile(REGEX_TELEPHONE);
    private Pattern compileMacaoPhone= Pattern.compile(REGEX_MACAO_PHONE);
    private Pattern compileHongkongPhone = Pattern.compile(REGEX_HONGKONG_PHONE);
    private Pattern compileTaiwanPhone = Pattern.compile(REGEX_TAIWAN_PHONE);
    private Pattern compileMainlandId= Pattern.compile(REGEX_MAINLAND_ID);
    private Pattern compileHmReviewId= Pattern.compile(REGEX_HM_REVIEW_ID);
    private Pattern compilePassport= Pattern.compile(REGEX_PASSPORT);
    private Pattern compileHongkongId= Pattern.compile(REGEX_HONGKONG_ID);
    private Pattern compileMacaoId= Pattern.compile(REGEX_MACAO_ID);
    private Pattern compileTaiwanReviewId= Pattern.compile(REGEX_TAIWAN_REVIEW_ID);
	/**
	 * 港澳台手机号
	 */
    private Pattern compileGAT = Pattern.compile(REGEX_GAT);

    private CheckCustomerInfoUtil() {
    }

	public static CheckCustomerInfoUtil getInstance() {
		return SingletonHolder.INSTANCE;
	}

	private static class SingletonHolder {
		private static final CheckCustomerInfoUtil INSTANCE = new CheckCustomerInfoUtil();
	}

	/**
	 * 检查输入联系方式是否符合规范
	 * 联系方式没有输入类型,按 大陆手机,固话,澳门,香港,台湾 顺序`或`逻辑校验
	 * @param phoneNumber 手机号码
	 * @return 是否通过校验
	 */
	public  boolean checkPhone(String phoneNumber) {
		return compileMobile.matcher(phoneNumber).matches() ||
				compileTelephone.matcher(phoneNumber).matches() ||
				compileMacaoPhone.matcher(phoneNumber).matches() ||
				compileHongkongPhone.matcher(phoneNumber).matches() ||
				compileTaiwanPhone.matcher(phoneNumber).matches()||
				compileGAT.matcher(phoneNumber).matches();
	}

	public static void main(String[] args) {

		//System.out.println(CheckCustomerInfoUtil.getInstance().checkPhone("6612345"));
		//System.out.println(Pattern.compile("^\\d{8}$").matcher("123456789").matches());
		List<String> strings = Arrays.asList("(\\d{10})\\([0-9|A-Z|a-z]\\)","^\\d{8}$");

		System.out.println(JsonUtils.toJsonString(strings));
		//System.out.println(strings);
	}

	/**
	 * 检查证件号码是否符合规范
	 * 按照证件名称取对应证件正则,取不到则不校验
	 * @param cardNumber 证件号码
	 * @param cardName 证件名称
	 * @return 是否校验通过
	 */
	public boolean checkCardNumber(String cardNumber, String cardName) {
		boolean cardPass;
		switch (cardName) {
			case "大陆身份证":
				cardPass = compileMainlandId.matcher(cardNumber).matches();
				break;
			case "港澳居民来往内地通行证":
				cardPass = compileHmReviewId.matcher(cardNumber).matches();
				break;
			case "护照":
				cardPass = compilePassport.matcher(cardNumber).matches();
				break;
			case "香港居民身份证":
				cardPass = compileHongkongId.matcher(cardNumber).matches();
				break;
			case "澳门居民身份证":
				cardPass = compileMacaoId.matcher(cardNumber).matches();
				break;
			case "台湾居民来往大陆通行证":
				cardPass = compileTaiwanReviewId.matcher(cardNumber).matches();
				break;
			default:
				cardPass = true;
				break;
		}
		return cardPass;
	}
}
